﻿using LD.Host;
using System;
using System.Collections.Generic;
using System.IO;

namespace LD
{
    public class PartNoHelper
    {
        public delegate void PartNoChangeHandle(string partNo);
        public event PartNoChangeHandle PartNoChangeEvent;
        private static PartNoHelper _instance;
        private static object _lock = new object();
        private string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\PartNo.config");
        private List<string> partNos = new List<string>();
        public static PartNoHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new PartNoHelper();
                        }
                    }
                }
                return _instance;
            }
        }
        public PartNoHelper()
        {
            Load();
        }
        /// <summary>
        /// 获取机种列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetPartNoList
        {
            get
            {
                return partNos;
            }
            set
            {
                partNos = value;
            }
        }
        public void Load()
        {
            if (File.Exists(filePath))
            {
                partNos = JsonSerializeHelper.Load<List<string>>(filePath);
            }
        }
        /// <summary>
        /// 新增机种
        /// </summary>
        /// <param name="partNo"></param>
        public void Add(string partNo)
        {
            partNos.Add(partNo);
            Save();
        }
        /// <summary>
        /// 保存机种
        /// </summary>
        public void Save()
        {
            JsonSerializeHelper.Save(partNos, filePath);
        }
        private PartNoModel _current;
        /// <summary>
        /// 当前机种
        /// </summary>
        public PartNoModel CurrentPartNo
        {
            get { return _current; }
            set
            {
                _current = value;
                PartNoChangeEvent?.Invoke(value.Name);
                Cache.SetCache("CurrentPartNo", value.Name);
                Cache.SaveCache();
            }
        }
        /// <summary>
        /// 加载机种
        /// </summary>
        /// <param name="partName"></param>
        public void Load(string partName)
        {
            string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\{partName}.config");

            if (File.Exists(_filePath))
            {
                if (CurrentPartNo != null)
                    LogHelper.Instance.ParamChange($"配方由【{CurrentPartNo.Name}】，切换为【{partName}】");

                CurrentPartNo = JsonSerializeHelper.Load<PartNoModel>(_filePath);
            }
        }
        public PartNoModel LoadPartNo(string partName)
        {
            string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\{partName}.config");

            if (File.Exists(_filePath))
            {
                return JsonSerializeHelper.Load<PartNoModel>(_filePath);
            }
            return null;
        }
        public void Copy(string sourceName,PartNoModel newPartNo)
        {
            string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\{sourceName}.config");

            if (File.Exists(_filePath))
            {
                var sourcePartNo = JsonSerializeHelper.Load<PartNoModel>(_filePath);
                sourcePartNo.Id = newPartNo.Id;
                sourcePartNo.Name = newPartNo.Name;
                sourcePartNo.CCDName = newPartNo.CCDName;
                sourcePartNo.Save();
                LogHelper.Instance.ParamChange($"配方由【{sourceName}】，复制到【{newPartNo.Name}】");

            }
        }
        public void Rename(string sourceName, PartNoModel newPartNo)
        {
            string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\{sourceName}.config");

            if (File.Exists(_filePath))
            {
                var partNo = JsonSerializeHelper.Load<PartNoModel>(_filePath);
                partNo.Id = newPartNo.Id;
                partNo.Name = newPartNo.Name;
                partNo.CCDName = newPartNo.CCDName;
                partNo.Save();
                //File.Delete(_filePath);
                LogHelper.Instance.ParamChange($"配方重命名由【{sourceName}】，修改为【{newPartNo.Name}】");
            }
        }
        public void Delete(string sourceName)
        {
            string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config\\{sourceName}.config");

            if (File.Exists(_filePath))
            {
                File.Delete(_filePath);
                partNos.Remove(sourceName);
                Save();
                LogHelper.Instance.ParamChange($"配方删除【{sourceName}】");
            }
        }
    }
}
